var fksh = {
    setBtnStatus: function (row) {
        if (row && row.id && row.zt) {
            //已保存 ybc  已入账  yrz   已审核  ysh   已提交  ytj  已退回 yth
            if (row.zt == 'ybc') {//已保存
                $("#btn_fksh_mod").show();//修改
                $("#btn_fksh_del").show();//删除
                $("#btn_fksh_rz").show();//入账
                $("#btn_fksh_tj").hide();//提交
                $("#btn_fksh_th").hide();//退回提交  领导权限
                $("#btn_fksh_sh").hide();//审核   领导权限
                $("#btn_fksh_nbhzsh").hide();//内部划转审核   领导权限
                $("#btn_fksh_qxsh").hide();//取消审核  领导权限
                $("#btn_fksh_xgshsj").hide();//修改审核时间
            } else if (row.zt == 'yrz') {//已入账
                $("#btn_fksh_mod").show();//修改
                $("#btn_fksh_del").show();//删除
                $("#btn_fksh_rz").show();//入账
                $("#btn_fksh_tj").show();//提交
                $("#btn_fksh_th").hide();//退回提交  领导权限
                $("#btn_fksh_sh").hide();//审核   领导权限
                $("#btn_fksh_qxsh").hide();//取消审核  领导权限
                $("#btn_fksh_nbhzsh").hide();//内部划转审核   领导权限
                $("#btn_fksh_xgshsj").hide();//修改审核时间
            } else if (row.zt == 'ysh') {//已审核
                $("#btn_fksh_mod").hide();//修改
                $("#btn_fksh_del").hide();//删除
                $("#btn_fksh_rz").hide();//入账
                $("#btn_fksh_tj").hide();//提交
                $("#btn_fksh_th").hide();//退回提交  领导权限
                $("#btn_fksh_sh").hide();//审核   领导权限
                $("#btn_fksh_nbhzsh").hide();//内部划转审核   领导权限
                $("#btn_fksh_qxsh").show();//取消审核  领导权限
                $("#btn_fksh_xgshsj").show();//修改审核时间
            } else if (row.zt == 'ytj') {//已提交
                $("#btn_fksh_mod").hide();//修改
                $("#btn_fksh_del").hide();//删除
                $("#btn_fksh_rz").hide();//入账
                $("#btn_fksh_tj").hide();//提交
                $("#btn_fksh_th").show();//退回提交  领导权限
                $("#btn_fksh_sh").show();//审核   领导权限
                $("#btn_fksh_nbhzsh").show();//内部划转审核   领导权限
                $("#btn_fksh_qxsh").hide();//取消审核  领导权限
                $("#btn_fksh_xgshsj").hide();//修改审核时间
            } else if (row.zt == 'yth') {//已退回提交
                $("#btn_fksh_mod").show();//修改
                $("#btn_fksh_del").show();//删除
                $("#btn_fksh_rz").show();//入账
                $("#btn_fksh_tj").show();//提交
                $("#btn_fksh_th").hide();//退回提交  领导权限
                $("#btn_fksh_sh").hide();//审核   领导权限
                $("#btn_fksh_nbhzsh").hide();//内部划转审核   领导权限
                $("#btn_fksh_qxsh").hide();//取消审核  领导权限
                $("#btn_fksh_xgshsj").hide();//修改审核时间
            } else {
                $("#btn_fksh_mod").hide();//修改
                $("#btn_fksh_del").hide();//删除
                $("#btn_fksh_rz").hide();//入账
                $("#btn_fksh_tj").hide();//提交
                $("#btn_fksh_th").hide();//退回提交  领导权限
                $("#btn_fksh_sh").hide();//审核   领导权限
                $("#btn_fksh_nbhzsh").hide();//内部划转审核   领导权限
                $("#btn_fksh_qxsh").hide();//取消审核  领导权限
                $("#btn_fksh_xgshsj").hide();//修改审核时间
            }
        } else {
            $("#btn_fksh_mod").hide();//修改
            $("#btn_fksh_del").hide();//删除
            $("#btn_fksh_rz").hide();//入账
            $("#btn_fksh_tj").hide();//提交
            $("#btn_fksh_th").hide();//退回提交  领导权限
            $("#btn_fksh_sh").hide();//审核   领导权限
            $("#btn_fksh_nbhzsh").hide();//内部划转审核   领导权限
            $("#btn_fksh_qxsh").hide();//取消审核  领导权限
            $("#btn_fksh_xgshsj").hide();//修改审核时间
        }
    },
    alwayShowDetail: function (rowData) {
        $('#fksh_layout').layout('remove', 'center');
        $('#fksh_layout').layout('add', {
            region: 'center',
            split: true,
            href: $.webPath() + '/sfgl/fksh/fksh_detail?fkshId=' + rowData.id
        });
    }
}

$(function () {
    //加载企业机构
    $("#fksh_gldw").gsjgqczcombobox();
    var clickRowIndex = null;
    var sysDate = $("#fksh_sysDate").val();
    //表格初始加载
    $('#grid_fksh').datagrid({
        url: $.webPath() + "/sfgl/fksh/fkshList",
        toolbar: "#toolbar_fksh",
        fitColumns: true,
        checkOnSelect: false,
        selectOnCheck: false,
        singleSelect: true,
        pagination: true,
        pageNumber: 1,
        fit: true,
        rownumbers: true,
        striped: true,
        columns: [
            [
                {checkbox: true},
                {field: 'bm', title: '单据号', width: 25,align: 'center'},
                {field: 'sfklx', title: '付款类型', width: 15,align: 'center',
                    formatter: function (value) {
                        if (value == '1')
                            return '供应付款';
                        else
                            return '销售退款';
                    }},
                {field: 'gldw_qc', title: '关联企业', width: 40,align: 'center'},
                {field: 'sfje', title: '付款金额（元）', width: 25,align: 'right',
                    formatter: function (value) {
                        var sl = formatJe2(value, ",");
                        return sl;
                    },
                    styler: function () {
                        return 'color:green;font-size:14px;';
                    }
                },
                {field: 'sfkrq', title: '付款日期', width: 20,align: 'center',
                    formatter: function (value) {
                        var val = formatDate(sysDate, value, 5);
                        return val
                    }},
                {field: 'kxlx', title: '款项类型', width: 15, align: 'center',
                    formatter: function (value) {
                        if (value == '1')
                            return '预付款';
                        if (value == '2')
                            return '应付款';
                        if (value == '3')
                            return '预收款';
                        if (value == '4')
                            return '应收款';
                    }
                },
                {field: 'pjlx_z', title: '票据类型', width: 15,align: 'center'},
                {field: 'sbgs_jc', title: '申报公司', width: 30,align: 'center'},
                {field: 'sbr_xm', title: '申报人', width: 15,align: 'center'},
                {field: "bz", title: "备注", width: 15,align: 'center'} ,
                {field: "shrq", title: "审核日期", width: 20,align: 'center'} ,
                {field: "zt", title: "状态", width: 10,align: 'center' ,
                    formatter: function (value, row) {
                        if (value == 'ybc') {
                            if (!row.processInstanceId)
                                return '已保存';
                            else
                                return '<a href="javascript:viewFkshFlowPicture();" style="color: red">' + '已保存' + '</a>';
                        } else if (value == 'yrz') {
                            return '已入账';
                        } else if (value == 'ysh') {
                            if (!row.processInstanceId)
                                return '已审核';
                            else
                                return '<a href="javascript:viewFkshFlowPicture();" style="color: red">' + '已审核' + '</a>';
                        } else if (value == 'ytj') {
                            if (!row.processInstanceId)
                                return '已提交';
                            else
                                return '<a href="javascript:viewFkshFlowPicture();" style="color: red">' + '已提交' + '</a>';
                        } else if (value == 'yth') {
                            return '已退回';
                        }
                    }}
            ]
        ],
        onLoadSuccess: function () {
            var rows = $(this).datagrid("getRows");
            if (rows.length > 0) {
                $(this).datagrid("selectRow", 0);
                clickRowIndex = 0;
                //fksh.setBtnStatus($(this).datagrid("getRows"));
            }
            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onSelect: function (rowIndex, rowData) {
            fksh.setBtnStatus(rowData);
            if (clickRowIndex == rowIndex)
                return;
            clickRowIndex = rowIndex;
            fksh.alwayShowDetail(rowData);

        }
    });

    //**********按钮触发事件**********
    //新增按钮
    $("#btn_fksh_addgyfk").click(function () {
        gyfk_add('新增供应付款申请', 'add_fksh');
    });

    $("#btn_fksh_addxstk").click(function () {
        xstk_add('新增其他付款申请', 'add_fksh');
    });


    //修改按钮
    $("#btn_fksh_mod").click(function () {
        fksh_modify('修改付款单', 'update_fksh');
    });
    //删除按钮
    $("#btn_fksh_del").click(function () {
        fksh_delete();
    });
    //审核按钮
    $("#btn_fksh_sh").click(function () {
        fksh_confirm();
    });

    //内部划转审核
    $("#btn_fksh_nbhzsh").click(function () {
        fksh_nbhz_confirm();
    });


    //提交按钮
    $("#btn_fksh_tj").click(function () {
        fksh_tj();
    });
    //退回提交
    $("#btn_fksh_th").click(function () {
        fksh_th();
    });
    //退回提交
    $("#btn_fksh_qxsh").click(function () {
        fksh_qxsh();
    });

    //修改审核时间
    $("#btn_fksh_xgshsj").click(function () {
        fksh_xgshsj('修改审核时间', 'fksh_xgshsj');
    });



    //入账按钮
    $("#btn_fksh_rz").click(function () {
        var row = $('#grid_fksh').datagrid('getSelected');
        if (row.pjlx_z == '银票' || row.pjlx_z == '商票') {
            fksh_check_pj('票据入账', 'fksh_sh_pj'); //如果类型是银票商票就需要弹出不一样的
        }
        if (row.pjlx_z == '信用证') {
            fksh_check_xyz('信用证入账', 'fksh_sh_xyz'); //如果类型是信用证
        }
        if (row.pjlx_z != '信用证' && row.pjlx_z != '银票' && row.pjlx_z != '商票') {
            fksh_check('现汇入账', 'fksh_sh');
        }
    });
    //查询按钮
    $("#btn_fksh_search").click(function () {
        fksh_search();
    });
    //双击事件
    $("#grid_fksh").datagrid({
        onDblClickCell: function (index, field) {
            $('#grid_fksh').datagrid('selectRow', index);
            var row = $('#grid_fksh').datagrid('getSelected');
            if (row.zt) {
                if (field == "bm" && row.zt != 3 && row.zt != 2) {
                    fksh_modify('修改付款单', 'update_fksh');
                }
            }
        }
    });
    //加载查询条件中的申报公司下拉框
    $("#fksh_sbgs").combotree({
        loadFilter: function (rows) {
            return convert(rows);
        }
    });
    //申请提现按钮
    $("#btn_fksh_tx").click(function () {
        var win = $.window({
            title: "申请提现",
            width: 620,
            height: 360,
            cache: false,
            url: $.webPath() + "/sfgl/fksh/fksh_tx_content",
            modal: true,
            buttons: [
                {
                    text: '保存',
                    handler: function () {
                        $(win).mask();
                        $('#form_fksh_tx').form('submit', {
                            url: $.webPath() + "/sfgl/fksh/fksh_tx",
                            onSubmit: function () {
                                if(!$("#fksh_dfejzh_id").val()) {
                                    $.messager.alert("提示","请选择二级账户","info");
                                    $(win).mask("hide");
                                    return false;
                                }
//                                var ye = $(this).find("[name='ejzhye']").val();
//                                var ye_input = $("#fksh_sfje").val();
//                                if(ye<ye_input) {
//                                    $.messager.alert("提示","账户余额不足","info");
//                                    $(win).mask("hide");
//                                    return false;
//                                }
                                if(!$(this).form("validate")){
                                    $(win).mask("hide");
                                    return false;
                                } else {
                                    return true;
                                }
                            },
                            success: function (data) {
                                $(win).mask('hide');
                                data = eval("("+data+")");
                                if (data.statusCode == 200) {
                                    $(win).window('close');
                                    $('#grid_fksh').datagrid('reload');
                                    $('#fksh_layout').layout('panel', 'center').panel('refresh');
                                    $.messager.show({
                                        title: '提示',
                                        msg: data.message,
                                        timeout: 3000,
                                        showType: 'slide'
                                    });
                                } else
                                    $.messager.alert('提示', data.message, 'error');
                            }
                        });
                    }
                }
            ],
            onBeforeClose: function () {
                $(".validatebox-tip").remove();
            }
        });
    });

});

//新增付款单
function gyfk_add(title, action) {
    var href = $.webPath() + "/sfgl/fksh/fksh_gyfk_content";
    var actionUrl = $.webPath() + "/sfgl/fksh/add_fksh";
    gyfk_showDialog(title, href, actionUrl);
}

//新增付款单
function xstk_add(title, action) {
    var href = $.webPath() + "/sfgl/fksh/fksh_xstk_content";
    var actionUrl = $.webPath() + "/sfgl/fksh/add_fksh";
    xstk_showDialog(title, href, actionUrl);
}


//修改付款单
function fksh_modify(title, action) {
    var record = $("#grid_fksh").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要更新的记录!", "warning");
        return;
    }
    if ($("#fksh_fklx").combobox("getValue") == 0 && record.qtfk == 0) {
        var href = $.webPath() + "/sfgl/fksh/fksh_gyfk_content?fkshId=" + record.id;
        var actionUrl = $.webPath() + "/sfgl/fksh/" + action;
        gyfk_showDialog(title, href, actionUrl);
    }if ($("#fksh_fklx").combobox("getValue") == 0 && record.qtfk == 1) {
        var href = $.webPath() + "/sfgl/fksh/fksh_xstk_content?fkshId=" + record.id;
        var actionUrl = $.webPath() + "/sfgl/fksh/" + action;
        xstk_showDialog(title, href, actionUrl);
    }else if ($("#fksh_fklx").combobox("getValue") == 1){
        var href = $.webPath() + "/jhgl/bgfkjh/bgfkjh_fksq?bgfkId=" + record.id;
        var actionUrl = $.webPath() + "/sfgl/fksh/update_bgfksh"
        bgfksh_showDialog(title, href, actionUrl);
    }

}
//审核应付款
function fksh_check(title, action) {
    var row = $('#grid_fksh').datagrid('getSelected');
    var href = $.webPath() + "/sfgl/fksh/" + action + "?fkshId=" + row.id;
    var actionUrl = $.webPath() + "/sfgl/fksh/check_fksh";
    fksh_sh_showDialog(title, href, actionUrl);
}
//审核应付款
function fksh_check_pj(title, action) {
    var row = $('#grid_fksh').datagrid('getSelected');
    var href = $.webPath() + "/sfgl/fksh/" + action + "?fkshId=" + row.id;
    var actionUrl = $.webPath() + "/sfgl/fksh/check_fksh_pj";
    fksh_sh_pj_showDialog(title, href, actionUrl);
}
//审核应付款(信用证)
function fksh_check_xyz(title, action) {
    var row = $('#grid_fksh').datagrid('getSelected');
    var href = $.webPath() + "/sfgl/fksh/" + action + "?fkshId=" + row.id;
    var actionUrl = $.webPath() + "/sfgl/fksh/check_fksh_xyz";
    fksh_sh_xyz_showDialog(title, href, actionUrl);
}

function fksh_xgshsj(title, action) {
    var record = $('#grid_fksh').datagrid('getSelected');
    if (record == null) {
        $.messager.alert("提示", "请选择要修改的销售时间!", "warning");
        return;
    }
    if (!record.zt == 'ysh' ) {
        $.messager.alert("提示", "请选择已经审核过的付款!", "warning");
        return;
    }
    var href = $.webPath() + "/sfgl/fksh/to_fksh_xgshsj?fkshId=" + record.id;
    var actionUrl = $.webPath() + "/sfgl/fksh/fksh_xgshsj";
    fksh_xgshsjDialog(title, href, actionUrl);
}


//弹出新增或修改的窗口
function gyfk_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 1024,
        height: 360,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_fksh').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            $("[name='fksh.zt']").val("ybc");
                            //结束编辑状态
                            $('#jkd_grid').edatagrid("saveRow");
                            var r = $('#jkd_grid').edatagrid("getSelected");
                            var index = $('#jkd_grid').edatagrid("getRowIndex", r);
                            $('#jkd_grid').edatagrid("endEdit", index);

                            var isValid = $(this).form('validate');
                            var data = $('#jkd_grid').edatagrid("getData");
                            $('#jkd_grid').datagrid('acceptChanges');
                            var jkdRows = $('#jkd_grid').datagrid('getRows');
                            for (var j = 0; j < jkdRows.length; j++) {
                                if ( Math.round((jkdRows[j]["bcfkmx"] / 1.17) * 100) / 100 >  Math.round(((jkdRows[j]["jkd.zfje"] - jkdRows[j]["jkd.yfje"]) / 1.17) * 100) / 100) {
                                    $(win).mask('hide');
                                    $("#form_fksh .tabs-title :eq(1)").click();
                                    $.messager.alert("提示", "本次借款不得大于借款余额!", "waring");
                                    return false;
                                }
                                if (!jkdRows[j]["bcfkmx"] || jkdRows[j]["bcfkmx"] == 0) {
                                    $(win).mask('hide');
                                    $("#form_fksh .tabs-title :eq(1)").click();
                                    $.messager.alert("提示", "请填写借款单本次付款金额!", "waring");
                                    return false;
                                }
                            }
                            var sfjezh = 0;
                            $.each(jkdRows, function (i, val) {
                                sfjezh += parseFloat(val.bcfkmx);
                            })
                            $('#fksh_sfje').numberbox('setValue', sfjezh);
                            if (!isValid) {
                                $(win).mask('hide');
                                $("#form_fksh .tabs-title :eq(0)").click();
                                return false;
                            } else if (!data.rows.length) {
                                $(win).mask('hide');
                                $.messager.alert("提示", "请选择电子借款单!", "waring");
                                $("#form_fksh .tabs-title :eq(1)").click();
                                return false;
                            }
                            var arrayPrefix = 'fksh.sfkmxList';
                            //物资计划明细
                            var paramString = "{";
                            //物资计划明细
                            for (var i = 0; i < data.rows.length; i++) {
                                var objectString = $.toJSONStringPrefix(data.rows[i], arrayPrefix + "[" + i + "].");
                                paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                            }
                            if (data.rows && data.rows.length > 0)
                                paramString = paramString.substring(0, paramString.length - 1);
                            paramString += "}";
                            $.extend(param, $.parseJSON(paramString));
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            if (data == "添加付款单成功!" || data == "修改付款单成功!") {
                                $(win).window('close');
                                $('#grid_fksh').datagrid('reload');
                                $('#fksh_layout').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: data,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', data, 'error');
                        }
                    });
                }
            }
        ],
        onBeforeClose: function () {
            $(".validatebox-tip").remove();
        }
    });
}

//弹出新增或修改的窗口
function xstk_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 1024,
        height: 360,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_fksh').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            $("[name='fksh.zt']").val("ybc");
                            var glwdid = $("#fksh_gldw_id").val();
                            if(!glwdid){
                                $.messager.alert("提示","请填写关联企业","info");
                                $(win).mask('hide');
                                return false;
                            }
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            if (data == "添加付款单成功!" || data == "修改付款单成功!") {
                                $(win).window('close');
                                $('#grid_fksh').datagrid('reload');
                                $('#fksh_layout').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: data,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', data, 'error');
                        }
                    });
                }
            }
        ],
        onBeforeClose: function () {
            $(".validatebox-tip").remove();
        }
    });
}


//弹出新增或修改的窗口
function bgfksh_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 800,
        height: 360,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_bgfk').form('submit', {
                        url: actionUrl,
                        onSubmit: function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            if (data == "修改保供付款单成功!") {
                                $(win).window('close');
                                $('#grid_fksh').datagrid('reload');
                                $('#fksh_layout').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: data,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', data, 'error');
                        }
                    });
                }
            }
        ],
        onBeforeClose: function () {
            $(".validatebox-tip").remove();
        }
    });
}

//弹出票据审核新增或修改的窗口
function fksh_sh_pj_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 750,
        height: 430,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_fksh_sh').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var rows = $("#fksh_pj_grid").datagrid("getRows");
                            var je = 0;
                            var ids = "";
                            if (rows) {
                                for (var i = 0; i < rows.length; i++) {
                                    je = je + parseFloat(rows[i].dqje);
                                    ids += rows[i].id + ",";
                                }
                                ids = ids.substring(0, ids.length - 1);
                                $("#pj_ids").val(ids);
                                if ($("#fksh_sfje").val() != je) {
                                    $(win).mask('hide');
                                    $.messager.alert('提示', '票据金额不等于付款金额', 'error');
                                    return false;
                                }
                                var isValid = $(this).form('validate');
                                if (!isValid) {
                                    $(win).mask('hide');
                                    return false;
                                }
                                return isValid;
                            } else {
                                $(win).mask('hide');
                                $.messager.alert('提示', '请选择应付票据', 'error');
                                return false;
                            }
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            if (data == "应付款已完成入账!") {
                                $(win).window('close');
                                $('#grid_fksh').datagrid('reload');
                                $('#fksh_layout').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: data,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', data, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//弹出信用证审核新增或修改的窗口
function fksh_sh_xyz_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 750,
        height: 430,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_fksh_sh').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var rows = $("#fksh_xyz_grid").datagrid("getRows");
                            var je = 0;
                            var ids = "";
                            if (rows) {
                                for (var i = 0; i < rows.length; i++) {
                                    je = je + parseFloat(rows[i].dqje);
                                    ids += rows[i].id + ",";
                                }
                                ids = ids.substring(0, ids.length - 1);
                                $("#xyz_ids").val(ids);
                                if ($("#fksh_sfje").val() != je) {
                                    $(win).mask('hide');
                                    $.messager.alert('提示', '信用证金额不等于付款金额', 'error');
                                    return false;
                                }
                                var isValid = $(this).form('validate');
                                if (!isValid) {
                                    $(win).mask('hide');
                                    return false;
                                }
                                return isValid;
                            } else {
                                $(win).mask('hide');
                                $.messager.alert('提示', '请选择信用证', 'error');
                                return false;
                            }
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            if (data == "应付款已完成入账!") {
                                $(win).window('close');
                                $('#grid_fksh').datagrid('reload');
                                $('#fksh_layout').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: data,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', data, 'error');
                        }
                    });
                }
            }
        ]
    });
}


//弹出现汇审核新增或修改的窗口
function fksh_sh_showDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 750,
        height: 430,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '确认',
                handler: function () {
                    $(win).mask();
                    $('#form_fksh_sh').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            if (data == "应付款已完成入账!") {
                                $(win).window('close');
                                $('#grid_fksh').datagrid('reload');
                                $('#fksh_layout').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: data,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', data, 'error');
                        }
                    });
                }
            }
        ]
    });
}

function fksh_xgshsjDialog(title, href, actionUrl) {
    var win = $.window({
        title: title,
        width: 420,
        height: 400,
        cache: false,
        url: href,
        modal: true,
        buttons: [
            {
                text: '保存',
                handler: function () {
                    $(win).mask();
                    $('#form_fksh_xgshsj').form('submit', {
                        url: actionUrl,
                        onSubmit: function () {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success: function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_fksh').datagrid('reload');
                                $('#fksh_layout').layout('panel', 'center').panel('refresh');
                                $.messager.show({
                                    title: '提示',
                                    msg: json.message,
                                    timeout: 3000,
                                    showType: 'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//删除付款单
function fksh_delete() {
    var row = $('#grid_fksh').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否删除该付款单？', function (r) {
            if (r) {
                var index = $('#grid_fksh').datagrid('getRowIndex', row);
                $.ajax({
                    url: $.webPath() + "/sfgl/fksh/delete_fksh",
                    dataType: 'json',
                    type: 'POST',
                    data: {fkshId: row.id},
                    beforeSend: function () {
                        $("#fksh_layout").mask();
                    },
                    success: function (data) {
                        $("#fksh_layout").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_fksh').datagrid('deleteRow', index);
                            $('#grid_fksh').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data.message,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要删除的付款单！", "warning");
    }
}

//审核付款单
function fksh_confirm() {
    var rows = $('#grid_fksh').datagrid('getChecked');
    var ids = '';
    for (var i = 0; i < rows.length; i++) {
        if (rows[i].zt != 'ytj') {
            $.messager.alert("提示", "只可以选择已提交的付款申请！", "warning");
            return;
        }
        ids = ids + rows[i].id + ",";
    }
    ids = ids.substring(0, ids.length - 1);
    if (rows.length > 0) {
        $.messager.confirm('提示', '是否审核付款单？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/sfgl/fksh/change_status",
                    dataType: 'text',
                    type: 'POST',
                    data: {fkshIds: ids, zt: 'ysh'},
                    beforeSend: function () {
                        $("#fksh_layout").mask();
                    },
                    success: function (data) {
                        $("#fksh_layout").mask("hide");
                        if (data == '付款单已审核!') {
                            $('#grid_fksh').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $("#fksh_layout").mask("hide");
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要审核付款单！", "warning");
    }
}

//审核付款单
function fksh_nbhz_confirm() {
    var rows = $('#grid_fksh').datagrid('getChecked');
    var ids = '';
    for (var i = 0; i < rows.length; i++) {
        if (rows[i].zt != 'ytj') {
            $.messager.alert("提示", "只可以选择已提交的付款申请！", "warning");
            return;
        }
        ids = ids + rows[i].id + ",";
    }
    ids = ids.substring(0, ids.length - 1);
    if (rows.length > 0) {
        $.messager.confirm('提示', '是否审核内部划转付款单？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/sfgl/fksh/change_status_nbhz",
                    dataType: 'text',
                    type: 'POST',
                    data: {fkshIds: ids, zt: 'ysh'},
                    beforeSend: function () {
                        $("#fksh_layout").mask();
                    },
                    success: function (data) {
                        $("#fksh_layout").mask("hide");
                        if (data == '付款单已审核!') {
                            $('#grid_fksh').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $("#fksh_layout").mask("hide");
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要内部划转审核付款单！", "warning");
    }
}

function fksh_tj() {
    var rows = $('#grid_fksh').datagrid('getChecked');
    var ids = '';
    for (var i = 0; i < rows.length; i++) {
        if (rows[i].zt != 'yrz' && rows[i].zt != 'yth') {
            $.messager.alert("提示", "只可以选择已入账或已退回的付款申请！", "warning");
            return;
        }
        ids = ids + rows[i].id + ",";
    }
    ids = ids.substring(0, ids.length - 1);
    if (rows.length > 0) {
        $.messager.confirm('提示', '是否提交付款单？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/sfgl/fksh/fksh_tj",
                    dataType: 'text',
                    type: 'POST',
                    data: {fkshIds: ids, zt: 'ytj'},
                    beforeSend: function () {
                        $("#fksh_layout").mask();
                    },
                    success: function (data) {
                        $("#fksh_layout").mask("hide");
                        if (data == '付款单已提交!') {
                            $('#grid_fksh').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要提交付款单！", "warning");
    }
}
function fksh_th() {
    var row = $('#grid_fksh').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否退回付款单？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/sfgl/fksh/fksh_th",
                    dataType: 'text',
                    type: 'POST',
                    data: {fkshId: row.id, zt: 'yth'},
                    beforeSend: function () {
                        $("#fksh_layout").mask();
                    },
                    success: function (data) {
                        $("#fksh_layout").mask("hide");
                        if (data == '付款单已退回!') {
                            $('#grid_fksh').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要退回付款单！", "warning");
    }
}

function fksh_qxsh() {
    var row = $('#grid_fksh').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否取消审核付款单？', function (r) {
            if (r) {
                $.ajax({
                    url: $.webPath() + "/sfgl/fksh/fksh_qxsh",
                    dataType: 'text',
                    type: 'POST',
                    data: {fkshId: row.id, zt: 'yth'},
                    beforeSend: function () {
                        $("#fksh_layout").mask();
                    },
                    success: function (data) {
                        $("#fksh_layout").mask("hide");
                        if (data == '付款单已取消审核!') {
                            $('#grid_fksh').datagrid('reload');
                            $.messager.show({
                                title: '提示',
                                msg: data,
                                timeout: 3000,
                                showType: 'slide'
                            });
                        } else {
                            $.messager.alert("提示", data, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要退回付款单！", "warning");
    }
}

//查询付款单
function fksh_search() {
    var sbgs = $('#fksh_sbgs').combotree('getText') == null ? null : $('#fksh_sbgs').combotree('getText');
    var sbgsId = $('#fksh_sbgs').combotree('getValue') == null ? null : $('#fksh_sbgs').combotree('getValue');
    var zt = $('#fksh_zt').combobox('getValue') == null ? null : $('#fksh_zt').combobox('getValue');
    var fklx = $('#fksh_fklx').combobox('getValue') == null ? null : $('#fksh_fklx').combobox('getValue');
    var fkshStart = $('#fksh_start').datebox('getValue');
    var fkshEnd = $('#fksh_end').datebox('getValue');
    $('#grid_fksh').datagrid('load',
        {
            bm: $('#fksh_bm').val(),
            sbgs: sbgs,
            sbgsId: sbgsId,
            gldwMc: $('#fksh_gldw').attr("_text") == null ? null : $('#fksh_gldw').attr("_text"),
            zt: zt,
            fklx: fklx,
            fkshStart:fkshStart,
            fkshEnd:fkshEnd,
            zdr:$("#fksh_zdr").val()
        }
    );
}


function viewFkshFlowPicture() {
    var row = $('#grid_fksh').datagrid('getSelected');
    $.window({
        title: "流程历史",
        iconCls: 'icon-add2',
        url: $.webPath() + "/flow/publish/flowPicture?processInstanceId=" + encodeURI(encodeURI(row.processInstanceId)) + "&businessNo=" + row.bm,
        width: 650,
        height: 500,
        closed: true,
        modal: true,
        isMax: true
    });
}
